//
//  PainterMACD.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/16.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterIndexMACD.h"

using namespace std;
using namespace ytx;

@implementation PainterIndexMACD

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    IndexData * data = (IndexData *)indexPosition->getData();

    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    float zoomX, x = rect.getBeginX();
    double item;
    string color = "";
    float w;
    float lineWidth = style.quoteLineWidth;
    float lineWidthHalf = lineWidth * 0.5f;
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();

    zoomX = rect.getZoomX();
    w = zoomX - lineWidthHalf;

    vector<double> macd = data->indexData[0];
    //含结束的下标
    for (vector<double>::iterator it = macd.begin() + startIndex; it != macd.begin() + endIndex; ++it) {
        item = (*it);
        if(fabs(item) < 0.000001)
        {
            x += zoomX;
            continue;
        }
        else if( item > 0 ){
            color = style.defaultRed;

        }
        else{
            color = style.defaultGreen;
        }

        [ctx fillRectWithX1:x + lineWidthHalf withY1:rect.getPxY(0) withX2:MAX( x + w - lineWidth, x + 1 ) withY2:rect.getPxY(item) withFillColor:[NSString stringWithUTF8String:color.c_str()]];

        x += zoomX;
    }
}


@end
